#!/bin/bash
Backup_All_Databases(){
bin_Path="/usr/local/mysql/bin"
my_user="root"
my_pass="666666"
backup_Dir="/opt"
socket_file="/tmp/mysql.sock"

#取数据库名
dbList=`mysql -u${my_user} -p$my_pass -S ${socket_file} -e "show databases;" | \
	egrep -vi "Database|information_schema|mysql|performance_schema"`

echo "即将备份以下数据库"
sleep 6
echo "$dbList"
sleep 3
echo "----------------------"
#备份上面的库，并且刷新log_bin日志
for db in $dbList
do
    ${bin_Path}/mysqldump -u$my_user -p$my_pass -S ${socket_file} -B -F \
	    --master-data=2 --single-transaction --events --compact \
	    ${db} | gzip >${backup_Dir}/mysql_backup_database_${db}_$(date +%Y%m%d-%H-%M-%S).sql.gz
    if [ $? = 0 ];then
        echo "${db}库备份成功"
	sleep 6
    else
        echo "${db}库备份失败，即将退出备份"
        exit 22
	sleep 6
    fi
done
}
#Backup_All_Databases
